package util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
    private static final ObjectMapper objectMapper;

    static {
        objectMapper = new ObjectMapper();

    }

    public static String beanToJson(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T jsonToBean(String json, Class<T> clazz) throws JsonProcessingException {
        if (json == null || json.length() == 0) {
            throw new IllegalArgumentException("json is null");
        }
        return objectMapper.readValue(json, clazz);

    }

}
